import requests, json, hashlib
from finance_c2c.local_settings import KuaiDi100Key
from finance_c2c.local_settings import KuaiDiCustomer


def kuai_di_result(post_company_code, num):
    key = KuaiDi100Key  # 客户授权key
    customer = KuaiDiCustomer  # 查询公司编号
    param = {}
    param['com'] = post_company_code  # 快递公司编码
    param['num'] = num  # 快递单号
    pjson = json.dumps(param)  # 转json字符串

    postdata = {}
    postdata['customer'] = customer  # 查询公司编号
    postdata['param'] = pjson  # 参数数据

    # 签名加密
    str = pjson + key + customer
    md = hashlib.md5()
    md.update(str.encode())
    sign = md.hexdigest()
    postdata['sign'] = sign.upper()  # 加密签名

    url = 'http://poll.kuaidi100.com/poll/query.do'  # 实时查询请求地址

    result = requests.post(url, postdata, verify=False)  # 发送请求
    return json.loads(result.content)
